<?php
require_once('Retrievable.interface.php');
require_once('Authenticate.interface.php');

/**
 * Base dom
 * Encapsulates common functionalities of doms
 *
 * @package adroit
 * @subpackage dom
 */
class BaseModel {
    
    /**
     * @var Integer Primary key
     */
    public $id;
    
    /**
     * @var Date Date/Time dom was created.  Initially set upon creation of dom
     */
    public $created;
    
    /**
     * Constructor - populates object with values passed in that are attributes of $this
     *
     * @param Array $values
     * @return BaseDom
     */
    public function __construct($values) {
        foreach($this as $key => $value) {
            if(@isset($values[$key])) {
                $this->$key = $values[$key];
            }
        }
        if($this->created == null) {
            $this->created = date('Y:m:d H:i:s');
        }
    }
        
    /**
     * Returns BaseDom matching the id passed in
     *
     * @param Integer $id
     * @param String $class
     * @return BaseDom
     */
    public static function get($id, $class) {
        $values = AdroitDB::fetchRecord(strtolower($class), 'id', $id);
        return new $class($values);
    }
    
    public static function getByField($field, $value, $class) {
        $values = AdroitDB::getDomByField(strtolower($class), $value, $field);
        if($values != null) {
            return new $class($values);
        }else {
            return null;
        }
    }

    /**
     * Returns an array of objects based on search/sort parameters passed in
     *
     * @param Array $searchArray
     * @param Array $sortArray
     * @return Array
     */
    public static function getAll($searchArray, $sortArray, $class) {
        $records = AdroitDB::fetchRecords(strtolower($class), $searchArray, $sortArray);
        $doms = Array();
        foreach($records AS $values) {
            $doms[] = new $class($values);
        }
        return $doms;
    }
    
    /**
     * Returns an array of objects based on the query passed in
     *
     * @param String $query
     * @param String $class
     * @return Array
     */
    public static function getByQuery($query, $class) {
        $records = AdroitDB::$dbase->fetchAssocArray($query);
        $doms = Array();
        foreach($records AS $values) {
            $doms[] = new $class($values);
        }
        return $doms;
    }

    /**
     * Inserts a new record into the table
     *
     * @return BaseDom
     */
    public function save() {
        AdroitDB::saveDom(strtolower(get_class($this)), $this, 'id');
        return $this;
    }
    
    /**
     * Deletes a record from the table based on the primary key id passed in
     *
     */
    public function delete() {
        AdroitDB::deleteRecord(strtolower(get_class($this)), 'id', $this->id);
    }
    
    /**
     * Updates a record in the table with the values passed in through the array
     *
     */
    public function update() {
        unset($this->created);
        AdroitDB::updateDom(strtolower(get_class($this)), $this, 'id');
    }
    
    /**
     * Determines if any matching entries exist for the given attribute.
     * Returns the matching Object if one is found, otherwise returns boolean true.
     *
     * @param String $fieldName
     * @param String $fieldValue
     * @return mixed
     */
    public function isUnique($field) {
        $class = get_class($this);
        return self::getByField($field, $this->$field, $class);
    }
}
?>